package HW.Polynomial;

/**************************************************************
* Class PolyTest
* @author Jeff Thomas - Thomas007
* @vesion 1.0.0
* Date: 02.15.2008
* Copyright 2008, Jeff Thomas
***************************************************************/

   import java.io.*;
   import java.util.LinkedList;
   import java.util.Iterator;

    public class PolyTest
   {
   /**************************************************************
   * Main
   * Tasks: 1. Build 2 Polynomial LinkedLists from user input
   *			2. Add Polynomials
   *			3. Display sum Polynomial
   * Side Effects: None
   ***************************************************************/
       public static void main(String[] args) throws IOException
      {
			//create to LinkedLists to hold our Polynomials
         LinkedList<Polynomial> poly1 = new LinkedList<Polynomial>();
         LinkedList<Polynomial> poly2 = new LinkedList<Polynomial>();
			//Build both Polynomials and assign them to poly1 and 2
         poly1 = buildPoly(inputDegree());
         poly2 = buildPoly(inputDegree());
			//Add the two polynomials and display them
         displayPoly(addPoly(poly1, poly2));
      }
   /**************************************************************
   * Input Degree
   * @return - Integer of user input degree
   * Tasks: 1. Use BufferedReader for user to input degree of poly
   * Side Effects: None
   ***************************************************************/
       public static int inputDegree() throws IOException
      {		
      	//bufferedreader for input of degree
         BufferedReader inStream = new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Enter polynomial degree: ");
			//assign the user input to degree
         int degree = Integer.parseInt(inStream.readLine());
         return degree;
      }
   /**************************************************************
   * Build Polynomial
   * @return - LinkedList of Polynomials
   * Tasks: 1. Create a Polynomial for each degree in equation
   *			2. Add that Polynomial to the Linked List
   * Side Effects: None
   ***************************************************************/
      public static LinkedList<Polynomial> buildPoly(int deg) throws IOException
      {
         LinkedList<Polynomial> polyList = new LinkedList<Polynomial>();
			//bufferedreader for input of coefficient
         BufferedReader inStream = new BufferedReader(new InputStreamReader(System.in));
			//while the degree is greater than 0, keep on creating terms
         while (deg >= 0)
         {
            System.out.print("Enter coefficient for the term x^" + deg + ": ");
            int coef = Integer.parseInt(inStream.readLine());
				//create our new Polynomial object
				Polynomial poly = new Polynomial(coef, deg);
				//add the polynomial to our linkedlist
            polyList.addLast(poly);
				//reduce degree by 1
            deg = deg - 1;
         }
         return polyList;
      }
   /**************************************************************
   * Display Polynomial
   * Tasks: 1. Print each Polynomial from the LinkedList
   * Side Effects: None
   ***************************************************************/	
       public static void displayPoly(LinkedList<Polynomial> polyList)
      {
         Polynomial poly = new Polynomial();
         Iterator<Polynomial> iter = polyList.iterator();
			//until we get to end of the linkedlist, keep on going	  
         while (iter.hasNext())
         {
            poly = iter.next();
				//don't print anything for a 0 polynomial
            if (poly.getCoefficient() == 0)
               System.out.print("");
				//don't print the + for the last term
            else if (!iter.hasNext())
               System.out.print(poly);
				//place a + between each term
            else
               System.out.print(poly + " + ");
         }
      }
   /**************************************************************
   * Add Polynomial
   * @return - Added Polynomial in a LinkedList
   * Tasks: 1. Create iterators for both LinkedLists of Polynomial
	*			2. While there are still Polys left, add ones with
	*				the same degrees together.
	*			3.	Add the terms with degree 0 together.
   * Side Effects: None
   ***************************************************************/	
       public static LinkedList<Polynomial> addPoly(LinkedList<Polynomial> polyList1, LinkedList<Polynomial> polyList2)
      {
			//create all of our variables to hold everything
         Polynomial sum = new Polynomial();
         Polynomial poly1 = new Polynomial();
         Polynomial poly2 = new Polynomial();
         LinkedList<Polynomial> sumList = new LinkedList<Polynomial>();
         Iterator<Polynomial> iter1 = polyList1.iterator();
         Iterator<Polynomial> iter2 = polyList2.iterator();
         poly1 = iter1.next();
         poly2 = iter2.next();
			//while we still have terms in either linkedlist
         while (iter1.hasNext() || iter2.hasNext())
         {
				//if the polynomials have equal degrees, add the coefficients, add it to the linkedlist,
				//and move both iterators
            if (poly1.compareTo(poly2) == 0)
            {
               sum = poly1.add(poly2);
               sum.setDegree(poly1.getDegree());
               sumList.add(sum);
               poly1 = iter1.next();
               poly2 = iter2.next();
            }
				// if poly1 is greater, add poly1 to the LinkedList and move the iterator
            else if (poly1.compareTo(poly2) == 1)
            {
               sumList.add(poly1);
               poly1 = iter1.next();
            }
				//if poly2 is greater, add poly2 to the linkedlist and move the iterator
            else
            {
               sumList.add(poly2);
               poly2 = iter2.next();
            }
         }
			//add our terms with 0 degree and put it in the linkedlist
         if (polyList1.getLast().getDegree() == 0 && polyList2.getLast().getDegree() == 0)
         {
            sum = poly1.add(poly2);
            sum.setDegree(poly1.getDegree());
            sumList.add(sum);
         }
         return sumList;
      }
   }
/**************************************************************
* I, Jeff Thomas, certify that each part of this program was
* written by me, except as noted within the code.
*
*						_________________________
***************************************************************/